import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd';


declare var form: any;
declare var map: any;

@Component({
  selector: 'admin-local',
  templateUrl: './admin.component.html',
  styleUrls: ['./admin.component.scss']
})
export class AdminLocationComponent implements OnInit {
  fs: any = null;
  geometry: any = null;

  constructor(private msg: NzMessageService) {
    // 加载政区
    this.fs = new form.FormTreeService('adminlocal', {
      orderObject: { id: 0 },
      keyField: 'id',
      randomFields: ['id'],
      pageSize: 1000,
      defaultObject: { leaf: false },
      queryFields: 'id,code,display,abbreviation,order,valid,info,pid,level,leaf'
    });
    // 加载表单数据
    this.fs.loadData();
  }

  ngOnInit() {
  }


  localByAdministrative(item: any) {
    const fs = new form.FormSimpleService('adminlocal', {
      orderObject: { id: 0 },
      keyField: 'id',
      randomFields: ['id'],
      pageSize: 1000,
      defaultQueryObject: { id: item.id }
    });

    fs.loadData(true, (data) => {
      if (data && data[0] && data[0].geoString) {
        // 异步请求，嵌套使用
        map.Factory.Utils('TerraformerUtils', (terraformerUtils) => {
          this.geometry = terraformerUtils.WktToGeoGeometry(data[0].geoString);
          let bounds: [number, number, number, number] = [360, 180, 0, 0];
          map.Factory.Utils('GeojsonUtils', (geojsonUtils) => {
            bounds = geojsonUtils.getGeometryBoundingBox(this.geometry);
            if (bounds === [360, 180, 0, 0]) {
              this.msg.error('空间信息错误');
            } else {
              if (this.geometry.type === 'Point') {
                form.ConfigService.map.zoomToPoint(
                  this.geometry.coordinates[0], this.geometry.coordinates[1], 14
                );
              } else {
                form.ConfigService.map.zoomToExtent(bounds);
              }
            }

          });
        });
      } else {
        this.msg.error('该地方没有空间信息');
      }
    });



  }




}
